import pandas as pd
import numpy as np

# def get_stroke(c):
#     # 如果返回 0, 则也是在unicode中不存在kTotalStrokes字段
#     strokes = []
#     txt_path = 'strokes.txt'
#     with open(txt_path, 'r') as fr:
#         for line in fr:
#             strokes.append(int(line.strip()))
#
#     unicode_ = ord(c)
#
#     if 13312 <= unicode_ <= 64045:
#         return strokes[unicode_ - 13312]
#     elif 131072 <= unicode_ <= 194998:
#         return strokes[unicode_ - 80338]
#     else:
#         print("c should be a CJK char, or not have stroke in unihan data.")
#         # can also return 0
#
# xlsx_name = '十行字库.xlsx'
# df_0 = pd.read_excel(xlsx_name)
# df_out = pd.DataFrame(columns=['十行', '汉字', '笔画', '除8余数'])
# for index, row in df_0.iterrows():
#     print(index)
#     dict_out = {}
#     shixng_tmp = row['十行']
#     hanzi_tmp = row['汉字']
#     print(hanzi_tmp)
#     bihua_tmp = get_stroke(hanzi_tmp)
#     bagua_tmp = bihua_tmp % 8
#     if bagua_tmp == 0:
#         bagua_tmp = 8
#     dict_out['十行'] = shixng_tmp
#     dict_out['汉字'] = hanzi_tmp
#     dict_out['笔画'] = bihua_tmp
#     dict_out['除8余数'] = bagua_tmp
#     new_row = pd.Series(dict_out)
#     df_out = df_out.append(new_row, ignore_index=True)
#
# writer = pd.ExcelWriter('十行字库豪华版.xlsx')
# df_out.to_excel(writer, sheet_name='Sheet1', index=False)
# writer.save()
# print('finish')
######################################################3
xlsx_name = '十行字库豪华版.xlsx'
df_0 = pd.read_excel(xlsx_name)
shixing_list = ['甲木', '乙木', '丙火', '丁火', '戊土', '己土', '庚金', '辛金', '壬水', '癸水']
bagua_list = [1, 2, 3, 4, 5, 6, 7, 8]
columns_list = ['列1', '列2', '列3', '列4', '列5', '列6', '列7', '列8', '列9', '列10',
                '列11', '列12', '列13', '列14', '列15', '列16', '列17', '列18', '列19', '列20']
df_out = pd.DataFrame(columns=columns_list)

for shixing_tmp in shixing_list:
    print(shixing_tmp)
    for bagua_tmp in bagua_list:
        print(bagua_tmp)
        dict_out = {}
        dict_out['列1'] = shixing_tmp + '_' + str(bagua_tmp)
        new_row = pd.Series(dict_out)
        df_out = df_out.append(new_row, ignore_index=True)
        df_0_tmp = df_0[(df_0['十行'] == shixing_tmp) & (df_0['除8余数'] == bagua_tmp)]
        df_0_tmp = df_0_tmp.reset_index(drop=True)
        hanzi_list_tmp = df_0_tmp['汉字'].tolist()
        for i in range(0,len(hanzi_list_tmp)):
            print(i)
            hanzi_tmp = hanzi_list_tmp[i]
            column_num = (i+1) % 20
            if column_num == 1:
                dict_out = {}
                column_name = '列' + str(int(column_num))
                dict_out[column_name] = hanzi_tmp
            elif column_num != 1 and column_num != 0:
                column_name = '列' + str(int(column_num))
                dict_out[column_name] = hanzi_tmp
            elif column_num == 0:
                column_num = 20
                column_name = '列' + str(int(column_num))
                dict_out[column_name] = hanzi_tmp
                new_row = pd.Series(dict_out)
                df_out = df_out.append(new_row, ignore_index=True)
            if i == len(hanzi_list_tmp)-1 and column_num != 20:
                new_row = pd.Series(dict_out)
                df_out = df_out.append(new_row, ignore_index=True)
        print(df_out)
writer = pd.ExcelWriter('十行字库豪华版v2.1.xlsx')
df_out.to_excel(writer, sheet_name='Sheet1', index=False)
writer.save()
















